@TomatoBreak = {}
$ = jQuery

@TomatoBreak.ControllerFactory =

    create: (element, klass, params = {}) ->
        params.el = element
        controller = new klass params
        element.data 'controller', controller
        controller

@TomatoBreak.changePage = (page) ->
    $.mobile.changePage page, { changeHash: false } if $.mobile.activePage

getController = (element) -> element?.data 'controller'

($ document).bind 'mobileinit', ->

    $.mobile.ajaxEnabled = false
    $.mobile.hashListeningEnabled = false
    $.mobile.loadingMessage = 'wait'

    opt = $.mobile.page.prototype.options
    opt.addBackBtn = false
    opt.degradeInputs.date = 'text'

    ($ '#main').live('pagehide', (event, ui) ->
                    (getController ui.nextPage)?.activate())
               .live('pageshow', (event, ui) ->
                    (getController ui.prevPage)?.reset())

    ($ document).ajaxSend(-> $.mobile.showPageLoadingMsg())
                .ajaxStop(-> $.mobile.hidePageLoadingMsg())